<?php

namespace App\V3\V3_2_6\Controllers;

use App\V3\V3_2_6\Services\UsersService;
use App\V3\V3_2_6\Models\AdminModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

class UsersController extends BaseController
{
    /**
     * 子账号列列表
     * @param Request $request
     * AUTHOR  : Sun Wu
     * DateTime: 2022/3/8 14:42
     * @return \Illuminate\Http\JsonResponse
     */
    public function userList(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'pageSize' =>'required|integer',//页面大小
            'currentPage' =>'required|integer',//当前页
        ], [
            'pageSize.required' => '参数`页面大小`丢失',
            'pageSize.integer' => '参数`页面大小`仅能为数字',
            'currentPage.required' => '参数`当前页码`丢失',
            'currentPage.integer' => '参数`当前页码`仅能为数字',
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        $data['keyword'] = empty($data['keyword'])?'':$data['keyword'];

        return $this->success(
            UsersService::userList($this->getMainUserId(), $data)
        );
    }

    public function transStatus(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'id' => 'required|integer',//用户id
            'status' => [
                'required',
                'integer',
                Rule::In([0,1]),
            ],//状态;1:启用;0:禁用;
        ],
            [
                'id.required' => '参数`用户id`缺失',
                'id.integer' => '参数`用户id`只能为整型',
                'status.required' => '参数`status`缺失',
                'status.integer' => '参数`status`只能为数字',
                'status.in' => '参数`status`不合法',
            ]
        );

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        //检查子账户是否存在
        $checkUser = UsersService::checkUser($data['id']);

        if(!$checkUser) {
            return $this->fail('', '用户不存在');
        }

        Redis::SET('user_id_'.$data['id'].'_disabled', 1);
        AdminModel::where('id', $data['id'])->update(['is_active' => $data['status']]);
        $this->log('禁用子账号', $data);
        return $this->success();
    }

    public function changePwd(Request $request)
    {
        $validator = Validator::make($data = $request->all(), [
            'mobile' => 'required|regex:/^1[3456789][0-9]{9}$/',
            'code' => 'required',
            'password' => 'required|confirmed'
        ], [
            'mobile.required' => '请填写手机号',
            'mobile.regex' => '手机号格式错误',
            'code.required' => '验证码不能为空',
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        $this->log('修改密码', $data);
        return response()->json(
            UsersService::changePwd($data)
        );
    }

    /**
     * 注销
     * AUTHOR  : Sun Wu
     * DateTime: 2022/7/28 14:33
     * @return \Illuminate\Http\JsonResponse
     */
    public function logOff()
    {
        UsersService::logOff($this->getUserId());
        return $this->success();
    }

    /**
     * 首页权限信息
     * @return \Illuminate\Http\JsonResponse
     * @Author: Sun Wu
     * @Created at: 2022/10/24 20:47
     */
    public function indexModuleInfo()
    {
        return $this->success(
            UsersService::indexModuleInfo(
                $this->getUserTpye(),
                $this->getUserId()
            )
        );
    }

    /**
     *开启自动出入库
     * Author: zlf
     * Created at 2023/3/9 11:29
     * @return void
     */
    public function autoStorageSwitch(Request $request){
        $validator = Validator::make($data = $request->all(), [
            'switch_flag' => 'required',
        ], [
            'switch_flag.required' => '参数`自动开启出入库开头标示`缺失',
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        $time = time();
        $this->operateLog('自动开启出入库',$data,$time);
        $data['user_id'] = $this->getUserId();
        return response()->json(
            UsersService::changeAutoStorageSwitch($data)
        );
    }
}
